/*
 * ActonManagerCase.cpp
 *
 *  Created on: 2012-9-8
 *      Author: yangentao@gmail.com
 */

#include "test.h"

#include "../ed/ActionManager.h"

class TextReplacable: public ITextReplacable {
public:
	String text;
	String textReplace(int from, int len, const String& newText) {
		String s;
		s = text.subString(from, len);
		text.replace(from, len, newText);
		return s;
	}
};

TEST(ActionManager, doAction){
	TextReplacable tr;
	tr.text = "abcd";
	ActionManager am(&tr);
	am.doAction(Action(1, 1, "B"));

	EXPECT_TRUE(tr.text.equals("aBcd"));
	EXPECT_TRUE(am.canUndo());
	EXPECT_FALSE(am.canRedo());
}

TEST(ActionManager, undo1){
	TextReplacable tr;
	tr.text = "abcd";
	ActionManager am(&tr);
	am.doAction(Action(1, 1, "B"));

	EXPECT_TRUE(tr.text.equals("aBcd"));
	EXPECT_TRUE(am.canUndo());
	EXPECT_FALSE(am.canRedo());
	am.undo();
	EXPECT_TRUE(tr.text.equals("abcd"));
}

TEST(ActionManager, undo2){
	TextReplacable tr;
	tr.text = "abcd";
	ActionManager am(&tr);
	am.doAction(Action(1, 1, "B"));//aBcd
	am.doAction(Action(0, 1, 0));//Bcd
	am.undo();//aBcd
	am.undo();//abcd
	EXPECT_TRUE(tr.text.equals("abcd"))<<tr.text.toStdString();//abd
	EXPECT_FALSE(am.canUndo());
}

TEST(ActionManager, redo1){
	TextReplacable tr;
	tr.text = "abc";
	ActionManager am(&tr);
	am.doAction(Action(1, 1, "B"));//aBc
	am.undo();//abc
	am.redo();//aBc
	EXPECT_TRUE(tr.text.equals("aBc"))<<tr.text.toStdString();//abd
}
TEST(ActionManager, redo2){
	TextReplacable tr;
	tr.text = "abc";
	ActionManager am(&tr);
	am.doAction(Action(0, 1, "A"));//Abc
	am.doAction(Action(1, 1, "B"));//ABc
	am.doAction(Action(2, 1, "C"));//ABC
	am.undo();//ABc
	am.undo();//Abc
	EXPECT_TRUE(am.canUndo());
	am.redo();//ABc
	am.redo();//ABC
	EXPECT_TRUE(tr.text.equals("ABC"))<<tr.text.toStdString();//abd
}


TEST(ActionManager, group1){
	TextReplacable tr;
	tr.text = "abc";
	ActionManager am(&tr);
	am.doAction(Action(0, 1, "A"));//Abc
	am.beginGroup();
	am.doAction(Action(1, 1, "B"));//ABc
	am.doAction(Action(2, 1, "C"));//ABC
	am.endGroup();
	am.undo();//Abc
	am.undo();//abc
	EXPECT_FALSE(am.canUndo());
	EXPECT_TRUE(tr.text.equals("abc"));

	am.redo();//Abc
	EXPECT_TRUE(tr.text.equals("Abc"));
	am.redo();//ABC
	EXPECT_TRUE(tr.text.equals("ABC"))<<tr.text.toStdString();//abd
}
